﻿try
{
    //var rootFolder = Directory.GetCurrentDirectory();
    Console.WriteLine($"请输入绝对路径：");
    var rootFolder = Console.ReadLine();
    if (!Directory.Exists(rootFolder))
    {
        Console.WriteLine("该路径不存在。");
    }

    Console.WriteLine($"是否删除packages文件夹（y/n）：");

    var hasDelPackages = Console.ReadLine() == "y";
    Console.WriteLine("-----------开始清理当前路径下obj文件夹和bin文件夹------------");
    DeleteObjBin(rootFolder, hasDelPackages);
    Console.WriteLine("-------------------已完成清理--------------------");
    Console.ReadLine();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
    Console.Read();
}

static void DeleteObjBin(string rootPath, bool hasDelPackages = false)
{
    try
    {
        string binFolderPath = Path.Combine(rootPath, "bin");
        string objFolderPath = Path.Combine(rootPath, "obj");



        if (Directory.Exists(binFolderPath))
        {
            Directory.Delete(binFolderPath, true);
            Console.WriteLine($"删除文件夹：{binFolderPath}");
        }
        if (Directory.Exists(objFolderPath))
        {
            Directory.Delete(objFolderPath, true);
            Console.WriteLine($"删除文件夹：{objFolderPath}");
        }
        if (hasDelPackages)
        {
            string packagesFolderPath = Path.Combine(rootPath, "packages");
            if (Directory.Exists(packagesFolderPath))
            {
                Directory.Delete(packagesFolderPath, true);
                Console.WriteLine($"删除文件夹：{packagesFolderPath}");
            }
        }

        var subFolders = Directory.GetDirectories(rootPath);
        foreach (var folder in subFolders)
        {
            DeleteObjBin(folder);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}
